<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>东宇个人博客 - 一个java小码农的博客网站</title>
<meta name="keywords" content="个人博客,东宇个人博客,个人博客模板,东宇" />
<meta name="description" content="东宇个人博客，是一个java码农的个人网站。" />
<link href="../../../images/logo.ico" type=image/x-icon rel="shortcut icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../../../css/base.css" rel="stylesheet">
<link href="../../../css/about.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="/skin/2017/js/modernizr.js"></script>
<![endif]-->
<script src="../../../js/scrollReveal.js"></script>
</head>
<body>
<header>
  <div class="logo"  data-scroll-reveal="enter right over 1s">
<a href="/"><img src="../../../images/logo.png"></a></div>
  <nav class="topnav" >
<a href="../../../home.html"><span>首页</span><span class="en">Home</span></a><a href="../../../about.html"><span>关于我</span><span class="en">About</span></a><a href="../../../manshenghuo.html"><span>慢生活</span><span class="en">Life</span></a><!--<a href="learn.html"><span>碎言碎语</span><span class="en">Doing</span></a>--><a href="../../../mbfx.html"><span>心情分享</span><span class="en">Share</span></a><a href="../../../learn.html"><span>学无止境</span><span class="en">Learn</span></a><a href="../../../gbook.html"><span>留言</span><span class="en">Saying</span></a>
</nav>
</header>
<article>
  <div class="container">
   
    <div class="about left">

    <ul> 
		<h4 class="atitle"><span>2018-1-9</span>设计模式(七)————————Iterator模式学习笔记</h4>
		<h2>结构</h2>
		<p>&nbsp;&nbsp;&nbsp;迭代器模式用于数组集合中按照顺序来遍历集合，方便输出。效果跟for的使用方式差不多，此方法就相当于指针，通过next方法指向下一个对象。<br><br>
		以下示例类图:</p>
		<p><img src="img/Iterator.png" alt="图片2" width="700" /></p>
		<p>
		Iterator:迭代器接口，主要是定义了hasNext和next俩个方法。<br>
		BookShelfIterator:具体的书架中书本的迭代类接口，实现了Iterator接口。同时，也依赖了BookShelf书架类。<br>
		Aggregate:集合接口，定义了Iterator。<br>
		BookShelf:书架类实现类集合，用来创建了自定义的迭代器类。<br>
		Book:书本对象，包含名字<br>
		</p>
		<h2>代码实现</h2>
		<h3>Iterator类</h3>
		<p>
		public interface Iterator {<br>
		&nbsp;&nbsp;public abstract boolean hasNext();<br>
		&nbsp;&nbsp;public abstract Object next();<br>
		}<br>
		</p>
		<h3>BookShelfIterator类</h3>
		<p>
		public class BookShelfIterator implements Iterator{<br>
		&nbsp;&nbsp;private BookShelf bookshelf;<br>
		&nbsp;&nbsp;private int index;<br>
			
		&nbsp;&nbsp;public BookShelfIterator(BookShelf bookshelf){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;this.bookshelf = bookshelf;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;this.index =0;<br>
		&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;@Override<br>
		&nbsp;&nbsp;public boolean hasNext() {<br>
		&nbsp;&nbsp;&nbsp;&nbsp;if(index < bookshelf.getLength()){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;}else{<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;}<br>

		&nbsp;&nbsp;@Override<br>
		&nbsp;&nbsp;public Object next() {<br>
		&nbsp;&nbsp;&nbsp;&nbsp;Book book = bookshelf.getBookAt(index);<br>
		&nbsp;&nbsp;&nbsp;&nbsp;index ++;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;return book;<br>
		&nbsp;&nbsp;}<br>

		}<br>

		</p>
		<h3>Aggregate类</h3>
		<p>
		public interface Aggregate {<br>
		&nbsp;&nbsp;public abstract Iterator iterator();<br>
		}<br>
		
		</p>
		<h3>BookShelf类</h3>
		<p>
		public class BookShelf implements Aggregate{<br>
		&nbsp;&nbsp;private Book[] books;<br>
		&nbsp;&nbsp;private int last = 0;<br>
		&nbsp;&nbsp;public BookShelf(int maxsize){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;this.books = new Book[maxsize];<br>
		&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;public Book getBookAt(int index){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;return books[index];<br>
		&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;public void appendBook(Book book){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;this.books[last] = book;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;last++;<br>
		&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;public int getLength(){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;return last;<br>
		&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;@Override<br>
		&nbsp;&nbsp;public Iterator iterator() {<br>
			 
		&nbsp;&nbsp;&nbsp;&nbsp;return new BookShelfIterator(this);<br>
		&nbsp;&nbsp;}<br>

		}<br>
		</p>
		<h3>Book类</h3>
		<p>
		public class Book {<br>
		&nbsp;&nbsp;private String name;<br>
			
		&nbsp;&nbsp;public Book(String name){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;<br>
		&nbsp;&nbsp;}<br>
			
		&nbsp;&nbsp;public String getName(){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;return name;<br>
		&nbsp;&nbsp;}<br>
		}<br>
		</p>
		<h3>Main类</h3>
		<p>
		public class Main {<br>
		&nbsp;&nbsp;public static void main(String[] args){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;BookShelf bookShelf = new BookShelf(4);<br>
		&nbsp;&nbsp;&nbsp;&nbsp;bookShelf.appendBook(new Book("大型网站分布式架构"));<br>
		&nbsp;&nbsp;&nbsp;&nbsp;bookShelf.appendBook(new Book("分布式网站与中间件"));<br>
		&nbsp;&nbsp;&nbsp;&nbsp;bookShelf.appendBook(new Book("架构探险"));<br>
		&nbsp;&nbsp;&nbsp;&nbsp;bookShelf.appendBook(new Book("spring内幕"));<br>
		&nbsp;&nbsp;&nbsp;&nbsp;Iterator it = bookShelf.iterator();<br>
		&nbsp;&nbsp;&nbsp;&nbsp;while(it.hasNext()){<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Book book = (Book)it.next();<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(book.getName());<br>
		&nbsp;&nbsp;&nbsp;&nbsp;}<br>
		&nbsp;&nbsp;}<br>
		}<br>

		</p>
		<h3>运行结果</h3>
		<p><img src="img/iterator1.png" alt="图片3" width="700" /></p>
		<p>
		上述迭代器的具体实现是通过Iterator的hasNext和next方法，不依赖于具体实现(BookShelfIterator类)，方便于拓展和自定义规则。这样也可以理解,为什么在BookShelfIterator类中的iterator方法中返回的是Iterator。<br>
		</p>
		
    <div class="keybq">
	<p><span>关键字词</span>：设计模式、Iterator、迭代器</p>    
   
<div class="gbko">
<!--高速版-->
<div id="SOHUCS" sid="789" ></div>
<!-- 来必力City版安装代码 -->
<div id="lv-container" data-id="city" data-uid="MTAyMC8zMjYwOS85MTcw">
	<script type="text/javascript">
   (function(d, s) {
       var j, e = d.getElementsByTagName(s)[0];

       if (typeof LivereTower === 'function') { return; }

       j = d.createElement(s);
       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       j.async = true;

       e.parentNode.insertBefore(j, e);
   })(document, 'script');
	</script>
<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
</div>
<!-- City版安装代码已完成 -->
</div>
</div>

</div>
  </div>
  </aside>
</article>
<footer> Design by DanceSmile <a href="/"></a> </footer>
<script>
	if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))){
		(function(){
			window.scrollReveal = new scrollReveal({reset: true});
		})();
	};
</script><script src="/e/public/onclick/?enews=donews&classid=11&id=789"></script></body>
</html>
